package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Dict;
import com.atguigu.result.Result;
import com.atguigu.service.DictService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @PACKAGE_NAME: com.atguigu.controller
 * @CLASSNAME: DictController
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/20 21:59
 * @SINCE 17.0.7
 * @DESCRIPTION: DictController
 */
@Controller
@RequestMapping("/dict")
public class  DictController {
    private static final String PAGE_INDEX = "dict/index";

    @Reference
    private DictService dictService;

    @GetMapping
    public String index() {
        return PAGE_INDEX;
    }

    @RequestMapping("/findZnodes")
    @ResponseBody
    public Result<List<Map<String, Object>>> findZnodesByParentId(@RequestParam(value = "id", defaultValue = "0") Long id) {
        List<Map<String, Object>> znodes = dictService.findZnodes(id);
        Result<List<Map<String, Object>>> ok = Result.ok(znodes);
        return ok;
    }

    /**
     * 根据节点编码获取子节点列表
     *
     * @param dictCode
     * @return
     */
    @RequestMapping("/findListByDictCode/{dictCode}")
    @ResponseBody
    public Result<List<Dict>> findListByDictCode(@PathVariable("dictCode") String dictCode) {
        List<Dict> dictList = dictService.findListByDictCode(dictCode);
        Result<List<Dict>> ok = Result.ok(dictList);
        return ok;
    }

    @RequestMapping("/findListByParentId/{parentId}")
    @ResponseBody
    public Result<List<Dict>> findListByParentId(@PathVariable("parentId") Long parentId) {
        List<Dict> dictList = dictService.findListByParentId(parentId);
        return Result.ok(dictList);
    }

}
